package com.realtyManage.springboot.Service.impl;

import com.realtyManage.springboot.Model.Do.SpecialOfferHouse;
import com.realtyManage.springboot.Service.ISpecialOfferHouseService;
import com.realtyManage.springboot.Dao.SpecialOfferHouseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * SpecialOfferHouseServiceImpl
 *
 * @Author 杭州素色网络科技有限公司
 * @Date 2019/5/15
 **/
@Service
@Transactional
public class SpecialOfferHouseServiceImpl implements ISpecialOfferHouseService {
    @Autowired
    private SpecialOfferHouseRepository specialOfferHouseRepository;
    @Autowired
    private ISpecialOfferHouseService specialOfferHouseService;


    /*@Override
    public SpecialOfferHouse getOne(Integer id) {
        SpecialOfferHouse specialOfferHouse = articleRepository.getOne(id);
        if(specialOfferHouse==null || specialOfferHouse.getState()!=1){
            return null;
        }else{
            return  specialOfferHouse;
        }
    }*/

    @Override
    public SpecialOfferHouse save(SpecialOfferHouse specialOfferHouse) {
        Integer qg = (Integer.valueOf(specialOfferHouse.getHandsel())*100);
        specialOfferHouse.setHandsel(qg.toString());
        return specialOfferHouseRepository.save(specialOfferHouse);
    }

    /*@Override
    public int deleteById(Integer id){
        return specialOfferHouseRepository.deleteById(id);
    }*/

    @Override
    public List<SpecialOfferHouse> delete(SpecialOfferHouse specialOfferHouse) {
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(specialOfferHouse.getId());
        List<SpecialOfferHouse> so = specialOfferHouseRepository.findAllById(ids);
        specialOfferHouseRepository.deleteInBatch(so);
        return so;
    }

    @Override
    public List<SpecialOfferHouse> getTjHouse(String hid){
        return specialOfferHouseRepository.getTjHouse(hid);
    }

    @Override
    public List<SpecialOfferHouse> censusSpecialOfferHouseInfo(Integer relevanceHouseId) {
        return specialOfferHouseRepository.findByRelevanceHouseId(relevanceHouseId);
    }

    @Override
    public int countByRelevanceHouseId(Integer relevanceHouseId){
        return  specialOfferHouseRepository.houseCount(relevanceHouseId);
    }

    @Override
    public List<SpecialOfferHouse> findSpecialOfferHouseById(String id) {
        return specialOfferHouseRepository.findSpecialOfferHouseById(id);
    }

    @Override
    public SpecialOfferHouse updateSpecialOfferHouseById(SpecialOfferHouse specialOfferHouse) {
        return specialOfferHouseRepository.updateSpecialOfferHouse(specialOfferHouse);
    }

    @Override
    public List<SpecialOfferHouse> findSpecialOfferHouseByHouseId(String houserId) {
        return specialOfferHouseRepository.findSpecialOfferHouseByHouseId(houserId);
    }

    @Override
    public SpecialOfferHouse updateSpecialOfferHouseType(SpecialOfferHouse specialOfferHouse) {
        return specialOfferHouseRepository.saveAndFlush(specialOfferHouse);
    }

    @Override
    public SpecialOfferHouse getIdAll(Integer hid){
        return specialOfferHouseRepository.getIdAll(hid);
    }

    @Override
    public SpecialOfferHouse getIdAllOne(Integer hid){
        return specialOfferHouseRepository.getId(hid);
    }

    @Override
    public  int updateZfTasks(Integer id){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sTime = sdf.format(new Date());
        Integer zt = 1;
        return  specialOfferHouseRepository.updateZfTasks(id,sTime,zt);
    }

    @Override
    public int scanZt(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String eTime = sdf.format(new Date());
        String ids ="";
        //查询为进行有效支付的房源
        List<SpecialOfferHouse> list =  specialOfferHouseRepository.scanZt();
        for (int i= 0;i<list.size();i++){
            try {
                long p = getTime(list.get(i).getsTime(),eTime);
                long e = 15;
                if(p > e){
                    ids += list.get(i).getId()+",";
                }
                if (ids.length() > 0){
                    specialOfferHouseRepository.updateZt(ids.substring(0,ids.length()-1));
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }


    public static long getTime(String oldTime,String newTime) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long NTime =df.parse(newTime).getTime();
        //从对象中拿到时间
        long OTime = df.parse(oldTime).getTime();
        long diff=(NTime-OTime)/1000/60;
        return diff;
    }
}
